<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="$t('供应商参考链接')"
    width="900px"
    destroy-on-close
    :close-on-click-modal="false">
    <div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['distribution:skuUrl:add']">
            {{ $t('新增') }}
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="skuUrlList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column show-overflow-tooltip :label="$t('参考链接')" align="center" prop="asinCode">
          <template slot-scope="{ row }">
            <el-link :href="row.asinCode" target="_blank">
              {{ row.asinCode }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center" prop="remark" />
        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['distribution:skuUrl:edit']">
              {{ $t('修改') }}
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['distribution:skuUrl:remove']">
              {{ $t('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />

      <!-- 添加或修改商品SKU链接对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item :label="$t('参考链接')" prop="asinCode">
            <el-input v-model="form.asinCode" placeholder="https://www.amazon.co.jp/dp/B000V4OBBE" />
          </el-form-item>
          <el-form-item :label="$t('备注')">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">{{ $t('确定') }}</el-button>
          <el-button @click="cancel">{{ $t('取消') }}</el-button>
        </div>
      </el-dialog>
    </div>
  </el-dialog>
</template>

<script>
import { listSkuUrl, getSkuUrl, delSkuUrl, addSkuUrl, updateSkuUrl } from '@/api/distribution/sku/skuUrl'

export default {
  name: 'SkuUrl',
  data() {
    return {
      dialogTitle: '',
      showDialog: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 商品SKU链接表格数据
      skuUrlList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        skuId: undefined,
        skuNo: undefined,
        asinCode: undefined,
      },
      // 表单参数
      form: {
        asinCode: '',
        remark: '',
        skuId: '',
        skuNo: '',
      },
      // 表单校验
      rules: {
        asinCode: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  created() {
    // this.getList();
  },
  methods: {
    handleOpen({ title, skuInfo }) {
      this.dialogTitle = title
      const { skuId, skuNo } = skuInfo
      this.form.skuId = skuId
      this.form.skuNo = skuNo
      this.queryParams.skuId = skuId
      this.getList()
      this.showDialog = true
    },
    /** 查询商品SKU链接列表 */
    getList() {
      this.loading = true
      listSkuUrl(this.queryParams).then((response) => {
        this.skuUrlList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form.asinCode = ''
      this.form.remark = ''
      // this.form = {
      //   id: undefined,
      //   skuId: undefined,
      //   skuNo: undefined,
      //   asinCode: undefined,
      //   createBy: undefined,
      //   createTime: undefined,
      //   updateBy: undefined,
      //   updateTime: undefined,
      //   remark: undefined,
      // };
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.pageSize = 10
      this.queryParams.pageNum = 1
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const id = row.id || this.ids
      getSkuUrl(id).then((response) => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateSkuUrl(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addSkuUrl(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除商品SKU链接编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true
          return delSkuUrl(ids)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'distribution/skuUrl/export',
        {
          ...this.queryParams,
        },
        `skuUrl_${new Date().getTime()}.xlsx`,
      )
    },
  },
}
</script>
